<!--任务管理 - 新增编辑-->
<template>
    <div class="rightCenter">
        <div class="editTop">
            <div class="editTopTitle" @click="cancelClick()">任务管理</div>
            <i class="icon font_family icon-yema-you"></i>
            <div class="editTopTitle">{{editVal}}</div>
        </div>
        <div class="editList">
            <div class="editListName">
                任务名称：
            </div>
            <input class="editListInpBox fontBlue" v-model.trim='nameVal' type="text" placeholder="标题不超过50个字" maxlength="50">
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <div class="editList">
            <div class="editListName">
                头图：
            </div>
            <div class="editUpImg">
                <div>
                    <div class="editUpImgHand" v-show='!handImgVal'>
                        <i class="icon font_family icon-shangchuanzhaopian"></i>
                    </div>
                    <img class="editHandImg" v-show='handImgVal' :src="handImgVal" alt="">
                </div>
                <div>
                    <label for="fileUpImg">
                        上传
                    </label>
                    <input id="fileUpImg" accept="image/jpeg,image/jpg,image/png,image/bmp,image/gif" type="file" v-on:change="upImgChange($event)">
                    <p :class="{fontGary:!imgFontShow,fontRed:imgFontShow}">（图片大小660*510像素）</p>
                </div>
                <div class="warn" v-show='imgupShow'>
                    请上传头图
                </div>
            </div>
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>

        <!-- <div class="editList">
            <div class="editListName">
                任务类型：
            </div>
            <div class="editListInpFont">
                <el-select v-model="typeVal" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div> -->
        <div class="editList">
            <div class="editListName">
                任务奖励：
            </div>
            <div class="award">
                <el-select v-model="awardIdVal" placeholder="请选择">
                    <el-option v-for="item in awardIdList" :key="item.value" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
                <!-- 数量 -->
                <!-- <input class="awardInp" v-model="awardVal" type="text" placeholder="请输入"> -->
            </div>
            <span class="warn" v-show='awardShow'>请输入任务奖励数量</span>
        </div>
        <div class="editList">
            <div class="editListName">
                参与人数上限：
            </div>
            <input class="editListInpBox fontBlue" v-model.trim='limitVal' type=" 人数上限xt" placeholder="标题不超过50个字" maxlength="50">
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <div class="editList">
            <div class="editListName">
                任务配置：
            </div>
            <div class="award">
                <el-select v-model="appIdVal" placeholder="请选择">
                    <el-option v-for="item in appIdLists" :key="item.value" :label="item.name" :value="item.appId">
                    </el-option>
                </el-select>
                <!-- 数量 -->
                <!-- <input class="awardInp" v-model="awardVal" type="text" placeholder="请输入"> -->
            </div>
            <!-- <span class="warn" v-show='awardShow'>请输入任务奖励数量</span> -->
        </div>
        <!-- <div class="editList">
            <div class="editListName">
                任务配置：
            </div>
            <input class="editListInpBox fontBlue" v-model.trim='limitVal' type=" 人数上限xt" placeholder="标题不超过50个字" maxlength="50">
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div> -->

        <div class="editList">
            <div class="editListName">
                活动时间：
            </div>
            <div class="timeBox">
                <el-date-picker v-model="startTimeVal" :picker-options="pickerOptionsTime1" value-format="timestamp" type="datetime" placeholder="选择日期时间">
                </el-date-picker>
                至
                <el-date-picker v-model="expireTimeVal" :picker-options="pickerOptionsTime1" value-format="timestamp" type="datetime" placeholder="选择日期时间">
                </el-date-picker>
            </div>
        </div>
        <div class="footButton">
            <div class="addBut" @click='addClick'>
                保 存
            </div>
            <div class="cancelBut" @click='cancelClick'>
                取 消
            </div>
        </div>
    </div>
</template>
<script>
import { mapActions } from "vuex";
import fileRequest from "../../assets/file";
import prizeRequest from "../../assets/prize";
import useRequest from "../../assets/use";
import tasksRequest from "../../assets/tasks";
export default {
  data() {
    return {
      pickerOptionsTime1: {
        disabledDate: function(time) {
          return time.getTime() < Date.now();
        }
      },
      editVal: "编辑任务",
      limitVal: "", // 人数上限
      titleShow: false,
      nameVal: "",
      handImgVal: null, //头图
      imgupShow: false,
      imgFontShow: false, //头图限制文字
      awardVal: "", //任务奖励
      awardShow: false, //任务奖励
      awardIdVal: "", // 任务奖励类型
      awardIdList: [],
      appIdVal: "", // 任务配置app
      appIdLists: [],
      startTimeVal: "", //开始时间
      expireTimeVal: "" //结束时间
    };
  },
  filters: {},
  created: function() {
    let self = this;
    self.listFun();
  },
  watch: {},
  mounted() {},
  methods: {
    async listFun() {
      let self = this;
      await self.typeFun();
      await self.detailsFun();
    },
    async detailsFun() {
      let self = this;
      let params = {
        taskId: self.$route.query.id
      };
      let getParams = `taskId=${params.taskId}`;
      let res = await tasksRequest.gettaskinfo(getParams);
      if (res.statusCode == 200) {
        self.dataList = [];
        if (res.data) {
          self.nameVal = res.data.name;
          self.handImgVal = res.data.img || null;
          self.limitVal = res.data.limit;
          self.awardIdVal = res.data.awardId;
          self.appIdVal = res.data.appId;
          self.startTimeVal =
            res.data.startTime.length > 13
              ? res.data.startTime / 1000
              : res.data.startTime;
          self.expireTimeVal =
            res.data.expireTime.length > 13
              ? res.data.expireTime / 1000
              : res.data.expireTime;
        }
      } else {
        self.$message(res.statusMessage);
        self.dataList = [];
      }
    },
    async typeFun() {
      let self = this;
      let params1 = {
        name: "",
        page: 1,
        size: 200
      };
      let getParams1 = `name=${params1.name}&page=${params1.page}&size=${
        params1.size
      }`;
      let res1 = await prizeRequest.getWebAwardPage(getParams1);
      if (res1.statusCode == 200) {
        if (res1.data) {
          if (res1.data.records) {
            self.awardIdList = res1.data.records;
            self.awardIdVal = self.awardIdList[0].id;
          }
        }
      } else {
        self.$message(res.statusMessage);
        self.awardIdList = [];
      }
      let params2 = {
        page: 1,
        size: 2000
      };
      let getParams2 = `page=${params2.page}&size=${params2.size}`;
      let res2 = await useRequest.getappList(getParams2);
      if (res2.statusCode == 200) {
        if (res2.data) {
          if (res2.data.records) {
            self.appIdLists = res2.data.records;
            self.appIdVal = self.appIdLists[0].appId;
          }
        }
      } else {
        self.$message(res.statusMessage);
        self.appIdLists = [];
      }
    },
    //活动管理
    cancelClick() {
      this.$router.push({
        path: "/home/task"
      });
    },
    //选择图片
    upImgChange(e) {
      let self = this;
      let files = e.target.files || e.dataTransfer.files;
      let fileData = files[0];
      let reader = new FileReader();
      reader.onload = function(e) {
        let data = e.target.result;
        let image = new Image();
        image.onload = function() {
          // if (image.width !== 600 || image.height !== 510) {
          //     self.imgFontShow = true;
          //     self.$message({
          //         type: "info",
          //         message: "上传图片尺寸不符，请重新上传"
          //     });
          //     document.getElementById("fileUpImg").value = "";
          // } else if (fileData.size > 1048576) {
          //     self.imgFontShow = true;
          //     self.$message({
          //         type: "info",
          //         message: "上传图片大小过大，请重新上传"
          //     });
          //     document.getElementById("fileUpImg").value = "";
          // } else {
          //     self.imgFontShow = false;
          //     self.uploadBanner(files[0]);
          // }
          self.uploadBanner(files[0]);
        };
        image.src = data;
      };
      reader.readAsDataURL(fileData);
    },
    //上传banner图片
    async uploadBanner(file) {
      let self = this;
      let formdata = new FormData();
      formdata.append("file", file);
      let res = await fileRequest.getfileUploadFile(formdata);
      if (res.statusCode == 200) {
        self.$message({
          message: "上传图片成功",
          type: "success"
        });
        self.handImgVal = res.data;
        document.getElementById("fileUpImg").value = "";
      } else {
        self.$message(res.statusMessage);
        self.dataList = [];
      }
    },
    //保存
    async addClick() {
      let self = this;
      if (
        self.nameVal.length > 0 &&
        self.startTimeVal &&
        self.expireTimeVal &&
        new Date(self.startTimeVal).getTime() <
          new Date(self.expireTimeVal).getTime()
      ) {
        let params = {
          id: self.$route.query.id,
          appId: self.appIdVal,
          awardId: self.awardIdVal,
          expireTime: self.expireTimeVal,
          name: self.nameVal,
          startTime: self.startTimeVal
        };
        if (self.handImgVal) {
          params["img"] = self.handImgVal;
        }
        if (self.limitVal) {
          params["limit"] = self.limitVal;
        }
        let res = await tasksRequest.gettaskinsert(params);
        if (res.statusCode == 200) {
          self.$message("编辑任务成功！");
          self.cancelClick();
        } else {
          self.$message(res.statusMessage);
          self.dataList = [];
        }
      } else {
        if (self.nameVal.length == 0) {
          self.$message("任务名称必填！");
        } else if (!self.startTimeVal) {
          self.$message("开始时间必填！");
        } else if (!self.expireTimeVal) {
          self.$message("结束时间必填！");
        } else if (
          new Date(self.startTimeVal).getTime() >
          new Date(self.expireTimeVal).getTime()
        ) {
          self.$message("开始时间小于结束时间且必填！");
        }
      }
    }
  }
};
</script>
<style scoped>
.editHandImg {
  width: 200px;
  height: 100px;
}
.editUpImgHand {
  width: 200px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  background: #f2f5f7;
  color: #b2b7c0;
}

.editUpImgHand i {
  font-size: 20px;
}

.textBox {
  width: 350px;
  min-height: 80px;
  line-height: 24px;
  font-size: 14px;
  background: #f2f5f7;
  margin-left: 12px;
  padding: 4px 8px;
  margin-right: 14px;
  resize: none;
}

.timeBox {
  display: flex;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  margin-top: 10px;
  color: #2f3a5d;
}

.footButton {
  display: flex;
  width: 700px;
  align-items: center;
  margin-top: 124px;
  margin-left: 230px;
}

.addBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  background: #279de2;
  color: white;
}

.cancelBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  border: 1px solid #279de2;
  color: #279de2;
}

.award {
  height: 50px;
  line-height: 50px;
  margin-left: 12px;
}

.awardInp {
  width: 100px;
  height: 50px;
  padding: 0 12px;
  line-height: 50px;
  background: #f2f5f7;
  margin-left: 14px;
}
</style>